<?php
/**
 * design pattern 之 工厂模式 Factory
 */


// 简单工厂
class SimpleFactory
{
    public function getInstance($class)
    {
        return new $class();
    }
}

class spf
{
    public function show($name)
    {
        return $name;
    }
}

$factory = new SimpleFactory();
$spf1 = $factory->getInstance('spf');
echo $spf1->show('spf name');

// 静态工厂
class StaticFactory
{
    public static function getInstance($class)
    {
        return new $class();
    }
}
class stf
{
    public function show($name)
    {
        return $name;
    }
}
$stf1 = StaticFactory::getInstance('stf');
echo $stf1->show('stf name');

/**
 * 工厂方法
 */
// 产品
interface Product {
    public function show();
}
class ProductA implements Product {
    public function show()
    {
        return 'ProductA';
    }
}
class ProductB implements Product{
    public function show()
    {
        return 'ProductB';
    }
}

// 工厂
interface Factory {
    public function createProduct();
}

class FactoryA implements Factory
{
    public function createProduct()
    {
        return new ProductA();
    }
}
class FactoryB implements Factory
{
    public function createProduct()
    {
        return new ProductB();
    }
}

$fa = new FactoryA();
$goods = $fa->createProduct();
echo $goods->show();

$fb = new FactoryB();
$goods = $fb->createProduct();
echo $goods->show();

// 新增类的时候 不用修改原有工厂类



// 抽象工厂
class AbstractFactory
{

}